package com.chm.fw.ui;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chm.framwork.banner.Banner;
import com.chm.framwork.banner.BannerConfig;
import com.chm.framwork.banner.loader.ImageLoader;
import com.chm.fw.R;
import com.chm.fw.base.BaseActivity;
import com.chm.fw.utils.ValueUtils;

import java.util.ArrayList;
import java.util.List;

public class BannerActivity extends BaseActivity {

    private Handler handler;
    private Runnable runnable;

    private List<String> pics = new ArrayList<>();
    private Banner banner;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner);
        handler = new Handler();
        initView();
    }

    private void initView() {
        banner = findViewById(R.id.a_banner);
        banner.setImages(pics)
                .setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {
                        Glide.with(BannerActivity.this)
                                .load(ValueUtils.getString(path))
                                .into(imageView);
                    }
                })
                .setBannerStyle(BannerConfig.CIRCLE_INDICATOR)//显示圆形指示器
                .setIndicatorGravity(BannerConfig.CENTER)//设置指示器位置（没有标题默认为右边,有标题时默认左边）
                .isAutoPlay(true)//设置自动轮播，默认为true
                .setDelayTime(3000)//设置轮播时间
                .start();
    }

    @Override
    public void onStart() {
        super.onStart();
        loadPic();
        banner.startAutoPlay();
    }

    private void loadPic() {
        runnable = new Runnable() {
            @Override
            public void run() {
                pics.clear();
                pics.add("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=7858389,272351871&fm=26&gp=0.jpg");
                pics.add("http://img2.imgtn.bdimg.com/it/u=1693984672,2745011350&fm=214&gp=0.jpg");
                banner.update(pics);
            }
        };
        handler.postDelayed(runnable, 3000);
    }

    @Override
    protected void onStop() {
        super.onStop();
        handler.removeCallbacks(runnable);
        banner.stopAutoPlay();
    }
}
